﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class ChucNang_KhachHangThanThiet_KHTT_XemDanhSachYeuThich : System.Web.UI.Page
{
    int RowPerPage;
    int startIndex;
    int page;
    protected void Page_Load(object sender, EventArgs e)
    {
        GlobalFunctions gf = new GlobalFunctions();
        if (gf.IsKHTT() == 0 && gf.IsKHDC() == 0)
            Response.Redirect("../NguoiDung/NguoiDung_XemDanhSachSanPham.aspx");
        if (!this.IsPostBack)
        {
            btnLoai.Attributes["OnClick"] = "return confirm('Bạn có thực sự muốn loại các sản phẩm này!');";
            DataDSTableAdapters.TAIKHOANTableAdapter tkadapter = new DataDSTableAdapters.TAIKHOANTableAdapter();
            chkEmail.Checked = (Boolean)tkadapter.TAIKHOAN_LayGuiMail((int)Session["LoginID"]);
        }

        if (Request["page"] != null)
            page = int.Parse(Request["page"]);
        else
            page = 0;
        //gia tri trong application
        //int RowPerPage = (int)Application["RowPerPage"];

        //gia tri trong web.config
        RowPerPage = int.Parse(ConfigurationSettings.AppSettings["RowPerPage"]);
        startIndex = (page * RowPerPage);


        DataDSTableAdapters.SANPHAM_YEUTHICHTableAdapter spytadapter = new DataDSTableAdapters.SANPHAM_YEUTHICHTableAdapter();
        PageInterface1.currentPageName = GlobalFunctions.GetCurrentPageName();
        PageInterface1.currentPageNumber = page;
        PageInterface1.totalNumberOfPage = (int)spytadapter.SANPHAM_LayTongSanPhamYeuThichTheoCMND((string)Session["CMND"]) / RowPerPage;
        if ((int)spytadapter.SANPHAM_LayTongSanPhamYeuThichTheoCMND((string)Session["CMND"]) % RowPerPage != 0)
            PageInterface1.totalNumberOfPage++;

        //DataList_DanhSachSanPham.DataSource = spAdapter.SANPHAM_LayTatCa();

        // DataList_DanhSachSanPham.DataSource = spAdapter.SANPHAM_LayTatCa_Paged(startIndex,RowPerPage);

       // DataDSTableAdapters.SANPHAM_YEUTHICHTableAdapter spytadapter = new DataDSTableAdapters.SANPHAM_YEUTHICHTableAdapter();
        DataList_DanhSachSanPham.DataSource = spytadapter.SANPHAM_LaySPYeuThichTheoCMND((string)Session["CMND"], startIndex, RowPerPage);
        DataList_DanhSachSanPham.DataBind();
        /* DataListItem item = DataList_DanhSachSanPham.Items[0];
         HiddenField hidden = (HiddenField)item.FindControl("Id_TaiKhoan");
         if(hidden.Value=="")
             Response.Write("Khác");*/
    }
    protected void btnLoai_Click(object sender, EventArgs e)
    {
        GlobalFunctions gf = new GlobalFunctions();
        string[] chuoi =Request.Form.GetValues("chkXoa");
        DataDSTableAdapters.SANPHAMYEUTHICHTableAdapter spytadapter = new DataDSTableAdapters.SANPHAMYEUTHICHTableAdapter();
        for (int i = 0; i < chuoi.Length; i++)
        {
            int? kq=0;
            spytadapter.SANPHAMYEUTHICH_XoaTheoMaSanPham(gf.getCMND().Trim(),chuoi[i],ref kq);
        }
        Response.Write("<script>alert('Đã loại!');location.href='KHTT_XemDanhSachYeuThich.aspx?page="+page.ToString()+"';</script>");
    }
    protected void chkEmail_CheckedChanged(object sender, EventArgs e)
    {
        GlobalFunctions gf = new GlobalFunctions();
        DataDSTableAdapters.TAIKHOANTableAdapter tkadapter = new DataDSTableAdapters.TAIKHOANTableAdapter();
        tkadapter.TAIKHOAN_CapNhatGuiMail(gf.getLoginId(), chkEmail.Checked);
    }
}
